var mongoose = require('mongoose');

var config = require('../config/config');
var urlLogSchema = require('./log').urlLogSchema;
var userSchema = require('./user').userSchema;

var historySchema = require('./stock').historySchema;
var listSchema = require('./stock').listSchema;

mongoose.Promise = global.Promise;

mongoose.set('debug', true);

// mongoose.connect(mongodbUri);
let mongo_url = config.mongo_url;
let mongo_urls = config.mongo_urls;

const db = mongoose.createConnection(config.mongo_url);
// const db = mongoose.createConnection(config.mongo_urls, config.mongo_ops);  // 连接副本集时使用
db.on('error', err => {
    console.error('和数据库 ' + mongo_url + '的连接出现错误，err: ', err);
});

db.on('open', () => console.log('成功打开数据库 ' + mongo_url));

db.on('connected', err => {
    if (err) return console.error('连接数据库 ' + mongo_url + '失败，err: ', err);
    console.log('成功连接数据库 ' + mongo_url);
});

exports.db = db;
exports.urlLog = db.model('urlLog', urlLogSchema);
exports.user = db.model('user', userSchema);
exports.history = db.model('history', historySchema);
exports.list = db.model('list', listSchema);





